<?php
/**
 * 模块: 管理员模块
 * User: 文丹
 * Date: 2018/1/23
 * Time: 15:16
 */
namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;
use \think\Validate;
class Manager extends Power{

    /**
     *  管理员列表
     */
    public function index(){
        //查询超级管理员
        $superAdmin = model('Manager')->getSuperAdmin();
      //  print_r($superAdmin);die;
        /*查询出除超级管理员意外的其他管理员*/
        $managers = model('Manager')->getManagers();
        $count    = model('Manager')->count();
        $roles    = model('Role')->selectRoles();
        $this->assign('managers',$managers);
        $this->assign('count',$count);
        $this->assign('roles',$roles);
        $this->assign('superAdmin',$superAdmin);
        return $this->fetch('manager:index');
    }

    /**
     * 管理员信息
     */
    public function info(){
        $manager_id = session('managerId');

        $manager = model('Manager')->findManager($manager_id);
        $role    = Db::name('role')->where('id',$manager['role_id'])->find();
        $this->assign('manager',$manager);
        $this->assign('role',$role);
        return $this->fetch('manager:info');
    }
    /**
     *  管理员 搜索
     */
    public function managerSearch(){
        $change  = intval(input('change'));
        $datemin = trim(input('logmin'));
        $logmin  = strtotime($datemin);
        $datemax = trim(input('logmax'));
        $logmax  = strtotime($datemax);
        $account = trim(input('account'));
        $role_id = intval(input('role_id'));
        $superAdmin = model('Manager')->getSuperAdmin();
        $this->assign('superAdmin',$superAdmin);
        if($change==1){
            if(empty($logmin) && empty($logmax)){
                return 1;
            }else{
                $result = model('Manager')->searchTime($logmin,$logmax);
                $count  = model('Manager')->countTime($logmin,$logmax);

                $roles    = model('Role')->selectRoles();
                if(empty($result[0])){
                    return 2;
                }
                $this->assign('managers',$result);
                $this->assign('count',$count);
                $this->assign('roles',$roles);
                return $this->fetch('manager:index',['change'=>$change,'logmin'=>$datemin,'logmax'=>$datemax,'account'=>$account,'role_id'=>$role_id]);
            }
        }else if($change==2){
            if(empty($account)){
                return 1;
            }else{
                $result = model('Manager')->searchAccount($account);
                $count  = model('Manager')->countAccount($account);
                $roles    = model('Role')->selectRoles();
                if(empty($result[0])){
                    return 2;
                }
                $this->assign('managers',$result);
                $this->assign('count',$count);
                $this->assign('roles',$roles);
                return $this->fetch('manager:index',['change'=>$change,'logmin'=>$logmin,'logmax'=>$logmax,'account'=>$account,'role_id'=>$role_id]);
            }
        }else{
            $result    = model('Manager')->searchRole($role_id);
            $count     = model('Manager')->countRole($role_id);
            $roles     = model('Role')->selectRoles();
            $role_name = model('Role')->getRoleName($role_id);
            if(empty($result[0])){
                return 2;
            }
            $this->assign('managers',$result);
            $this->assign('count',$count);
            $this->assign('roles',$roles);
            $this->assign('role_name',$role_name);
            return $this->fetch('manager:index',['change'=>$change,'logmin'=>$logmin,'logmax'=>$logmax,'account'=>$account,'role_id'=>$role_id]);
        }


    }
    /**
     *  管理员添加 - 页面
     */
    public function add(){
        $id = intval(input('id'));
        if($id){
           $manager = model('Manager')->findManager($id);
           $role    = Db::name('role')->where('id',$manager['role_id'])->find();
           $this->assign('manager',$manager);
           $this->assign('role',$role);
        }
        $roles = model('Role')->selectRoles();
        $this->assign('roles',$roles);
        return $this->fetch('manager:add');
    }
    /**
     *  管理员 - 账号异步检测
     */
    public function checkAccount(){

        $account = trim(input('account'));
        $account = model('Manager')->findAccount($account);
        if(empty($account)){
            return 1;
        }else{
            return 2;
        }
    }
    /**
     * 管理员修改 - 账号异步验证
     */
   public function checkAccountX(){
       $account = trim(input('account'));
       $id      = intval(input('manager_id'));
       $account = model('Manager')->checkAccount($account,$id);
       if(empty($account)){
           return 1;
       }else{
           return 2;
       }
    }
    /**
     *  管理员添加 - 保存
     */
    public function save(){
       $account   = trim(input('account'));
       $password  = trim(input('password'));
       $password2 = trim(input('password2'));
       $roleId    = intval(input('roleId'));
        $rule = [
            'account'  => 'alphaDash|length:4,16',
            'password'   => 'alphaNum|length:6,14',
        ];

        $msg = [
            'account.alphaDash' => '账号只能为字母数字下划线',
            'account.length'     => '账号长度必须4~16个字符',
            'password.alphaNum'   => '密码必须是字母或数字',
            'password.length'  => '密码长度必须6~14位',
        ];
        $arr   = ['account'=>$account,'password'=>$password];
        $validate = new Validate($rule,$msg);
        $res   = $validate->check($arr);
        if(!$res){
           return $validate->getError() ;
        }
        if(empty($account)||empty($password)||empty($password2)||empty($roleId)){
            return 1;
        }
        if($password!=$password2){
            return 2;
        }
        if(!empty($account)&& !empty($password)&& !empty($password2)&& !empty($roleId)){
            $data   = ['account'=>$account,'password'=>md5($password),'role_id'=>$roleId,'admin_time'=>time()];
            $result = model('Manager')->saveManager($data);
            if($result){
                return 3;
            }else{
                return 4;
            }
        }

    }
    /**
     *  管理员 -修改
     */
    public function edit(){
        $account   = trim(input('account'));
        $password  = trim(input('password'));
        $password2 = trim(input('password2'));
        $roleId    = intval(input('roleId'));
        $id        = intval(input('id'));
        if(empty($account)||empty($password)||empty($password2)||empty($roleId)){
            return 1;
        }
        if($password!=$password2){
            return 2;
        }
        if(!empty($account)&& !empty($password)&& !empty($password2)&& !empty($roleId)){
            $data   = ['account'=>$account,'password'=>md5($password),'role_id'=>$roleId,'admin_time'=>time()];
            $result = model('Manager')->editManager($data,$id);
            if($result){
                return 3;
            }
        }
    }
    /**
     *  管理员 - 停用
     */
    public function stop(){
        $id = intval(input('id'));
        $row = model('Manager')->stop($id);
        if($row){
            return 1;
        }
    }
    /**
     *  管理员 - 启用
     */
    public function start(){
        $id = intval(input('id'));
        $row = model('Manager')->start($id);
        if($row){
            return 1;
        }
    }
    /**
     *  管理员 - 逻辑删除
     */
    public function deleteManager(){
        $id   = intval(input('id'));
        $row  = model('Manager')->deleteManager($id);
        if($row>0){
            return 1;
        }
    }

    /**
     *  管理员 - 回收站管理
     */
    public function recycleBin(){
        $managers = model('Manager')->getDelManager();
        $count    = model('Manager')->getDelCount();
        $this->assign('managers',$managers);
        $this->assign('count',$count);
        return $this->fetch('manager:recycle-bin');
    }
    /**
     *  管理员 - 恢复
     */
    public function enable(){
        $id   = intval(input('id'));
        $row  = model('Manager')->enableManager($id);
        if(!empty($row)){
            return 1;
        }
    }
    /**
     *  管理员 - 彻底删除
     */
    public function delete(){
        $id   = intval(input('id'));
        $row  = model('Manager')->removeManager($id);
        if($row>0){
            return ['msg'=>'删除成功！','ok'=>true];
        }else{
            return ['msg'=>'删除失败','ok'=>false];
        }
    }
}